﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace L.Manage
{
    public static class IocFactory
    {
        private static object locker = new object();
        private static UnityContainer _container;

        private static string ConfigFile = "Config\\l.manage.unity.config";

        public static UnityContainer Container
        {
            get
            {
                if (_container == null)
                {
                    lock (locker)
                    {
                        if (_container == null)
                        {
                            InitilizeConfig();
                        }
                    }
                }
                return _container;
            }
        }

        private static void InitilizeConfig()
        {
            _container = new UnityContainer();
            
            //基础数据维护的依赖注入配置
            LoadUnityConfigFile(_container, AppDomain.CurrentDomain.BaseDirectory + ConfigFile);

            //基础数据维护的AOP配置
            //LoadUnityConfigFile(_container, "~/unity.aop.infrastructure.config");
        }

        private static void LoadUnityConfigFile(IUnityContainer container, string exeConfigFilename)
        {
            ExeConfigurationFileMap infraFileMap = new ExeConfigurationFileMap
                                                       {
                                                           ExeConfigFilename = exeConfigFilename
                                                       };
            UnityConfigurationSection infraConfig = (UnityConfigurationSection)ConfigurationManager
                                                                                    .OpenMappedExeConfiguration(
                                                                                        infraFileMap,
                                                                                        ConfigurationUserLevel.None)
                                                                                    .GetSection("unity");
            if (infraConfig.Containers.Default != null)
            {
                infraConfig.Containers.Default.Configure(container);
            }
        }

        public static T Resolve<T>(params ResolverOverride[] overrides)
        {
            return Container.Resolve<T>(overrides);
        }

        public static T Resolve<T>(string name, params ResolverOverride[] overrides)
        {
            return Container.Resolve<T>(name, overrides);
        }

        public static object Resolve(Type t, params ResolverOverride[] overrides)
        {
            return Container.Resolve(t, overrides);
        }

        public static object Resolve(Type t, string name, params ResolverOverride[] overrides)
        {
            return Container.Resolve(t, name, overrides);
        }
    }
}
